﻿/// <reference path="D:\刘亚平Net资料\代码\私人代码\SmartForms\SmartForm2.0\SmartForm\SmartForm.Web\Scripts/jquery-1.7.2.min.js" />
/// <reference path="D:\刘亚平Net资料\代码\私人代码\SmartForms\SmartForm2.0\SmartForm\SmartForm.Web\Scripts/jquery.query.js" />
; (function ($) {
    var methods = {
        init: function () {
            $(this).find("[ITSM].wmpMultUserSelector").WmpSelect("user", { multy: true });//初始化 多选用户
            $(this).find("[ITSM].wmpSingleUserSelector").WmpSelect("user");//初始化 单选用户
            $(this).find("[ITSM].wmpSingleDeptSelector").WmpSelect("dept");//初始化 单选部门
            $(this).find("[ITSM][type=radioList] .container-radio").on("click", function () {
                if ($(this).parent().attr("disabled") != "disabled") {
                    $(this).siblings().find("a").removeClass("checked");
                    $(this).find("a").addClass("checked");
                }
            });
            ///初始化多选 控件
            $(this).find("[ITSM][type=checkboxlist] .container-radio").on("click", function () {
                if ($(this).parent("[type=checkboxlist]").attr("disabled") != "disabled") {
                    if ($(this).find("a").hasClass("checked")) {
                        $(this).find("a").removeClass("checked");
                    } else {
                        $(this).find("a").addClass("checked");
                    }
                }
            });
            $(this).find("[dataJson]").each(function () {
                var detailConfig = JSON.parse(decodeURIComponent($(this).attr("dataJson")));
            
                if (detailConfig.ModelType != "GridList")
                    return;
                var grid = $(this);
                $(this).find(".ProcBtn .add").click(function () {
                    if (detailConfig.RefControlID != "") {
                        $(this).OpenSmartForm("addFrom");
                    }
                });
                $(this).find(".ProcBtn .del").click(function () {
                    var checkItemList = grid.find(".ckeckItem a.checked");
                    if (checkItemList.length < 1) {
                        parent.ShowError("请选择删除的行！");
                        return;
                    }
                });
                $(this).on("click", "a.Delete", function () {
                    //var trobj = $(this).parents("tr");
                    //grid.wmpDetail("remove_row", trobj);
                });
                $(this).on("click", "a.Edit", function () {
                    if (detailConfig.RefControlID != "") {
                        var trobj = $(this).parents("tr");
                        var rowobj = JSON.parse(decodeURI($(trobj).attr("tr_json_val")));
                        $(this).OpenSmartForm("editFrom");
                    }
                });
                $(this).find(".ProcBtn .import").click(function () {

                });
                $(this).find(".ProcBtn .export").click(function () {

                });
            });
            $(this).find("select[ITSM]").each(function () {
                var setting = {};
                switch ($(this).attr("EditType")) {
                    case "Single": setting.multy = false; break;
                    case "Multi": setting.multy = true; break;
                    case "SingleQuery": setting.multy = false; setting.search = true; break;
                    case "MultiQuery": setting.multy = true; setting.search = true; break;
                    default: setting.multy = false; break;
                }
                $(this).WmpSelectBox("init", setting);
            });
            $(this).find("[ITSM][type=project]").WmpSelect("project");
            $(this).find("[ITSM][type=cost]").WmpSelect("cost");
            $(this).find("[ITSM][itsmtype=TextDateTime]").wmpDate("calendar", {});
            $(this).find("[ITSM][itsmtype=editor]").each(function () {
                var plane = $(this).parent();
                var $this = $(this);
                if ($(this).attr("IsInit") == undefined) {
                    var editorID = "editor" + GetID();
                    plane.find(".editor").attr("id", editorID);
                    if (window.editor == undefined) {
                        window.editor = {};
                    }
            
                    if (parent.UE != undefined) {
                        window.editor[$(this).attr("id")] = {};
                        window.editor[$(this).attr("id")] = parent.UE.getEditor(editorID, {
                            initialFrameHeight: 200,
                            initialFrameWidth: "100%"
                        });
                    } else {
                        window.editor[$(this).attr("id")] = {};
                        window.editor[$(this).attr("id")] = UE.getEditor(editorID, {
                            initialFrameHeight: 200,
                            initialFrameWidth: "100%"
                        });
                    }
                    //Ueditor
                    window.editor[$(this).attr("id")].addListener("blur", function (editor) {
                        $this.val(parent.UE.getEditor(editorID).getContent());
                    });
                    if ($this.val() != "") {
                        window.editor[$this.attr("id")].ready(function () {
                            window.editor[$this.attr("id")].setContent($this.val());
                        });
                    }
                    $(this).attr("IsInit", "1");
                }
            })
            function InputControl($this) {
                ////输入限制控制
                $this.on("keyup", "[InputType=1]", function () {
                    if (this.value.length == 1) { this.value = this.value.replace(/[^1-9]/g, '') } else { this.value = this.value.replace(/\D/g, '') };
                });
                $this.on("paste", "[InputType=1]", function () {
                    if (this.value.length == 1) { this.value = this.value.replace(/[^1-9]/g, '') } else { this.value = this.value.replace(/\D/g, '') }
                });
                ////输入限制控制
                $this.on("keyup", "[InputType=2]", function () {
                    var regStrs = [
                    ['^0(\\d+)$', '$1'], //禁止录入整数部分两位以上，但首位为0  
                    ['[^\\d\\.]+$', ''], //禁止录入任何非数字和点  
                    ['\\.(\\d?)\\.+', '.$1'], //禁止录入两个以上的点  
                    ['^(\\d+\\.\\d{2}).+', '$1'] //禁止录入小数点后两位以上  
                    ];
                    for (var i = 0; i < regStrs.length; i++) {
                        var reg = new RegExp(regStrs[i][0]);
                        this.value = this.value.replace(reg, regStrs[i][1]);
                    }
                });
                ////输入限制控制
                $this.on("onBlur", "[InputType=2]", function () {
                    var v = this.value;
                    if (v === '') {
                        v = '0.00';
                    } else if (v === '0') {
                        v = '0.00';
                    } else if (v === '0.') {
                        v = '0.00';
                    } else if (/^0+\d+\.?\d*.*$/.test(v)) {
                        v = v.replace(/^0+(\d+\.?\d*).*$/, '$1');
                        v = inp.getRightPriceFormat(v).val;
                    } else if (/^0\.\d$/.test(v)) {
                        v = v + '0';
                    } else if (!/^\d+\.\d{2}$/.test(v)) {
                        if (/^\d+\.\d{2}.+/.test(v)) {
                            v = v.replace(/^(\d+\.\d{2}).*$/, '$1');
                        } else if (/^\d+$/.test(v)) {
                            v = v + '.00';
                        } else if (/^\d+\.$/.test(v)) {
                            v = v + '00';
                        } else if (/^\d+\.\d$/.test(v)) {
                            v = v + '0';
                        } else if (/^[^\d]+\d+\.?\d*$/.test(v)) {
                            v = v.replace(/^[^\d]+(\d+\.?\d*)$/, '$1');
                        } else if (/\d+/.test(v)) {
                            v = v.replace(/^[^\d]*(\d+\.?\d*).*$/, '$1');
                            ty = false;
                        } else if (/^0+\d+\.?\d*$/.test(v)) {
                            v = v.replace(/^0+(\d+\.?\d*)$/, '$1');
                            ty = false;
                        } else {
                            v = '0.00';
                        }
                    }
                    this.value = v;
                });
                $this.on("paste", "[InputType=2]", function () {
                    return;
                });
                $this.on("keyup", "[InputType=3]", function () {
                    this.value = this.value.replace(/[^\a-\z\A-\Z]/g, '');
                })
                $this.on("paste", "[InputType=3]", function () {
                    this.value = this.value.replace(/[^\a-\z\A-\Z]/g, '');
                })
                $this.on("contextmenu", "[InputType=3]", function () {
                    this.value = this.value.replace(/[^\a-\z\A-\Z]/g, '');
                });
                $this.on("keyup", "[InputType=4]", function () {
                    this.value = this.value.replace(/[^d|chun]/g, '')
                })
                $this.on("keyup", "[InputType=5]", function () {
                    this.value = this.value.replace(/[^\u4E00-\u9FA5]/g, '')
                });
                $this.on("paste", "[InputType=5]", function () {
                    this.value = this.value.replace(/[^\u4E00-\u9FA5]/g, '');
                });
                $this.on("contextmenu", "[InputType=5]", function () {
                    this.value = this.value.replace(/[^\u4E00-\u9FA5]/g, '');
                });

            }
            InputControl($(this));

        }


    }
    $.fn.SmartFormList = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method' + method + 'does not exist');
        }
    };
})($);
